<template>
	<view class="parent">
		<view class="login-title">{{loginTitle}}</view>

		<view class="login-input">
			<view class="label"> 手机号 </view>
			<input class="et" placeholder="请输入手机号" v-model="account" placeholder-class="holder"/>
		</view>

		<view class="login-input" v-if="2==loginType">
			<view class="label">验证码</view>
			<input class="et" placeholder="请输入验证码" v-model="code" placeholder-class="holder"/>
			<sm-code :account='account'></sm-code>
		</view>

		<view class="login-input" v-if="1==loginType">
			<view class="label">密码</view>
			<input class="et" placeholder="请输入密码" v-model="password" password placeholder-class="holder"/>
		</view>

		<view class="register">
			<view class="textButton" @click="loginTypeChange">{{loginTypeTitle}}</view>
			<view class="textButton" @click="goRegister">注册</view>
		</view>

		<lq-button text="立即登录" @click.native="requestLogin" style="margin-top: 100rpx;"></lq-button>
		<lq-privacy ref="privacy"></lq-privacy>
	</view>
</template>

<script>
	import LoginUtil from '@/common/LoginUtil.js'
	import RouteUtil from '@/common/RouteUtil.js'
	import md5 from '@/common/md5.js'
	export default {
		data() {
			return {
				loginType: '2',
				loginTitle: '手机号登录',
				loginTypeTitle: '账号密码登录',
				account: '',
				password: '',
				code: '',
			}
		},
		methods: {
			async requestLogin() {
				if (!this.$refs.privacy.value()) {
					this.$common.showToast('请勾选隐私政策')
					return
				}
				let param = {};
				param['loginname'] = this.account;
				param['loginType'] = this.loginType;
				if (1 == this.loginType) {
					param['password'] = md5.hex_md5(this.password);
				} else {
					param['code'] = this.code;
				}
				let res = await this.$common.post('/app/login/login', param);
				if (0 == res.code) {
					LoginUtil.saveToken(res.data.token)
					LoginUtil.login(res)
				} else {
					this.$common.showToast(res.msg)
				}
			},
			loginTypeChange() {
				if (1 == this.loginType) {
					this.loginType = 2;
					this.loginTitle = '手机号登录';
					this.loginTypeTitle = '账号密码登录';
				} else {
					this.loginType = 1;
					this.loginTitle = '密码登录';
					this.loginTypeTitle = '短信登录';
				}
			},
			goRegister() {
				RouteUtil.toRegister()
			},
		}
	}
</script>

<style lang="scss" scoped>
	.parent {
		padding: 0 50rpx;

		.login-title {
			color: #fff;
			font-size: 50rpx;
			font-weight: bold;
			padding-top: 260rpx;
		}

		.login-input {
			display: flex;
			flex-direction: row;
			align-items: center;
			background-color: rgba(#000, 0.5);
			border-radius: 10rpx;
			height: 90rpx;
			margin-top: 50rpx;

			.label {
				width: 150rpx;
				height: 100%;
				line-height: 90rpx;
				font-size: 32rpx;
				color: #888;
				text-align: center;
				margin: auto 0;
			}

			.et {
				height: 100%;
				padding: 0 20rpx;
				font-size: 28rpx;
				flex: 1;
				color: #fff;
			}
			
			.holder{
				color: #555;
			}
		}

		.register {
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;

			.textButton {
				height: 50rpx;
				line-height: 50rpx;
				padding: 20rpx 20rpx;
				color: #888;
				font-size: 30rpx;
			}
		}
	}
</style>